/*
 DNN Content Builder - sourceforge.net/projects/dnncb
 Copyright (c) 2004-2008
 by Emerson Lopes (emersonlopes@gmail.com)

 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
 documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
 the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
 to permit persons to whom the Software is furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in all copies or substantial portions 
 of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
 TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
 DEALINGS IN THE SOFTWARE.
*/
using System;
using System.Data;
using System.Xml;

namespace DNNContentBuilder
{
	/// <summary>
	/// Summary description for TypeEvaluator.
	/// </summary>
	public class TypeEvaluator
	{
		#region Member Variables
		private  string xmlValue;
		private  FieldDefinitionInfo fdInfo;
		#endregion
		
		#region Constants
		private const string resource = "~/DesktopModules/DNNContentBuilder/App_LocalResources/ResourceEngine";
		#endregion

		#region Constructors
		public TypeEvaluator(string xmlValue, FieldDefinitionInfo fdInfo)
		{
			this.xmlValue = xmlValue;
			this.fdInfo = fdInfo;
		}
		#endregion

		#region Properties
		public string XmlValue
		{
			get
			{
				return xmlValue;
			}
		}

		public FieldDefinitionInfo FieldDefinitionInfo
		{
			get
			{
				return fdInfo;
			}
		}
		#endregion

		#region Static Methods
		public static DbType GetFieldDbType(System.Type type)
		{
			switch (type.FullName)
			{
				case "System.String":
					return DbType.AnsiString;

				case "System.Guid":
					return DbType.Guid;

				case "System.DateTime":
					return DbType.DateTime;

				case "System.Boolean":
					return DbType.Boolean;

				case "System.Int16":
				case "System.Int32":
				case "System.Single":
				case "System.Double":
				case "System.Decimal":
					return DbType.Decimal;

				default:
					throw new NotSupportedException("Unsupported field type");
			}
		}

		public static DbType GetFieldDbType(string xml, FieldDefinitionInfo fdInfo)
		{
			DbType fieldType;

			XmlNode typeNode = GetTypeNode(xml, fdInfo);

			// TODO: implement checks for any other, remaining types here.
			switch (typeNode.InnerText)
			{
				case "String":
					fieldType = DbType.AnsiString;
					break;

				case "Bool":
					fieldType = DbType.Boolean;
					break;

				case "DateTime":
					fieldType = DbType.DateTime;
					break;

				case "Int32":
					fieldType = DbType.Int32;
					break;

				case "Double":
					fieldType = DbType.Double;
					break;

				case "Decimal":
					fieldType = DbType.Decimal;
					break;

				default:
					throw new NotSupportedException(String.Format(Commons.GetLocalizedString("UNSUPPORTED_FIELD_TYPE", resource), typeNode.InnerText));
			}

			return fieldType;
		}

		private static XmlNode GetTypeNode(string xml, FieldDefinitionInfo fdInfo)
		{
			XmlDocument xd = new XmlDocument();
			xd.LoadXml(xml);
	
			XmlNode typeNode = xd.SelectSingleNode("//SystemType");
	
			if (typeNode == null)
			{
				throw new ApplicationException(String.Format(Commons.GetLocalizedString("VALUE_NODE_NOT_FOUND", resource), fdInfo.SourceField));
			}

			return typeNode;
		}
		
		public static string GetFieldFormat(DbType dbType) {
			string type = "";
			
			switch (dbType) {
				case DbType.AnsiString:
				case DbType.Guid:
					type = "quoted";
					break;
					
				case DbType.DateTime:
				case DbType.Boolean:
				case DbType.Decimal:
					type = "literal";
					break;
			}
			
			return type;
		}

		public static object GetFieldValue(string xml, FieldDefinitionInfo fdInfo)
		{
			return TypeEvaluator.GetFieldValue(xml, fdInfo.SourceField);
		}

		public static object GetFieldValue(string xml, string fieldName)
		{
			ArgumentValidator.CheckForNullReference(xml, "xml");
			ArgumentValidator.CheckForNullReference(fieldName, "fieldName");

			object fieldValue;
			XmlDocument xd = new XmlDocument();

			xd.LoadXml(xml);
	
			XmlNode valueNode = xd.SelectSingleNode("//Value");
			
			ArgumentValidator.AssertNotNull(valueNode, "VALUE_NODE_NOT_FOUND", fieldName);
	
			fieldValue = Commons.DecodeAngleBrackets(valueNode.InnerText);

			return fieldValue;
		}
		#endregion

		#region Methods
		public DbType GetFieldDbType()
		{
			return TypeEvaluator.GetFieldDbType(this.xmlValue, this.fdInfo);
		}

		public object GetFieldValue()
		{
			return TypeEvaluator.GetFieldValue(this.xmlValue, this.fdInfo);
		}
		#endregion
	}
}
